import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { ShipmentVO, ShipmentForm, ShipmentQuery } from '@/api/ymxbh/shipment/types';

/**
 * 查询订单管理列表
 * @param query
 * @returns {*}
 */

export const listShipment = (query?: ShipmentQuery): AxiosPromise<ShipmentVO[]> => {
  return request({
    url: '/ymxbh/shipment/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询订单管理详细
 * @param id
 */
export const getShipment = (id: string | number): AxiosPromise<ShipmentVO> => {
  return request({
    url: '/ymxbh/shipment/' + id,
    method: 'get'
  });
};

/**
 * 新增订单管理
 * @param data
 */
export const addShipment = (data: ShipmentForm) => {
  return request({
    url: '/ymxbh/shipment',
    method: 'post',
    data: data
  });
};

/**
 * 修改订单管理
 * @param data
 */
export const updateShipment = (data: ShipmentForm) => {
  return request({
    url: '/ymxbh/shipment',
    method: 'put',
    data: data
  });
};

/**
 * 删除订单管理
 * @param id
 */
export const delShipment = (id: string | number | Array<string | number>) => {
  return request({
    url: '/ymxbh/shipment/' + id,
    method: 'delete'
  });
};


/**
 * 在线生成
 * @param id
 */
export const onlineGenerate = (id: string | number, type: number,printType: number,markType: number): AxiosPromise<any> => {
  return request({
    url: '/ymxbh/shipment/generateLabel/' + id,
    method: 'get',
    params: {
      type: type,
      printType: printType,
      markType: markType
    }
  });
};
/**
 *  产品确认或驳回
 * @param id 
 * @param isConfirm 
 * @returns 
 */

export const confirmOrReject = (id: string | number, isConfirm: boolean) => {
  return request({
    url: '/ymxbh/product/isConfirm/' + id,
    method: 'get',
    params: {
      isConfirm: isConfirm
    }
  });
};

/**
 * 确认或驳回验收材料
 * @param id 验收材料ID
 * @param isConfirm true:确认, false:驳回
 */
export const confirmOrRejectMaterial = (id: string | number, status: number,casue: string): AxiosPromise<any> => {
  return request({
    url: '/ymxbh/details/isConfirm/' + id,
    method: 'get',
    params: {
      status: status,
      casue: casue
    }
  });
};

/**
 * 删除上传材料
 * @param id 订单ID
 * @param type 上传材料类型 0:外箱标, 1:产品标, 2:唛头 3:打印图
 */
export const delUploadPdf = (id: string | number, type: number,productId: number | string) => {
  return request({
    url: '/ymxbh/shipment/uploadPdf/' + id,
    method: 'delete',
    params: {
      type: type,
      productId: productId
    }
  });
};

/**
 * 获取工厂列表
 */
export const getFactoriesApi = () => {
  return request({
    url: '/ymxbh/shipment/getFactoryList',
    method: 'get'
  });
};

/**
 * 获取工厂账号列表
 */
export const getAccountsApi = (name: string) => {
  return request({
    url: '/ymxbh/shipment/getFactoryAccountList',
    method: 'get',
    params: {
      name: name
    }
  });
};

/**
 * 获取工厂员工列表
 */
export const getEmployeesApi = () => {
  return request({
    url: '/ymxbh/shipment//getEmployeeList',
    method: 'get'
  });
};

/**
 * 下发压缩包到工厂员工账号
 */
export const sendZipToFactory = (id: number, employeeId: number) => {
  return request({
    url: '/ymxbh/shipment/sendZip/' + id,
    method: 'get',
    params: {
      employeeId: employeeId
    }
  });
};

//参数MultipartFile file, boolean updateSupport
export const batchImportOrder = (file: any, updateSupport: boolean) => {
  const formData = new FormData();
  formData.append('file', file); // 必须使用 append 添加文件
  formData.append('updateSupport', String(updateSupport)); // 将 boolean 转 string 提交

  return request({
    url: '/ymxbh/shipment/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

//参数MultipartFile file, boolean updateSupport
export const batchImportProduct = (file: any, updateSupport: boolean,id: number | string) => {
  const formData = new FormData();
  formData.append('file', file); // 必须使用 append 添加文件
  formData.append('updateSupport', String(updateSupport)); // 将 boolean 转 string 提交
  formData.append('shipmentId', String(id));

  return request({
    url: '/ymxbh/product/importProductWithImages',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

/**
 * 同步订单列表
 */
export const syncOrderList = () => {
  return request({
    url: '/ymxbh/shipment/sync',
    method: 'get'
  });
};

/**
 * 生成发票
 */
export const createInvoice = (id: string | number) => {
  return request({
    url: '/ymxbh/shipment/generateInvoice/' + id,
    method: 'get'
  });
};

/**
 * 合并订单
 * @param ids 订单ID数组
 */
export const mergeShipments = (ids: Array<string | number>) => {
  return request({
    url: '/ymxbh/shipment/merge/' + ids.join(','),
    method: 'get'
  });
};

/**
 * 上传产品标
 */
export const uploadProductLabel = (id: string | number,url: string) => {
  return request({
    url: '/ymxbh/product/uploadProductLabel',
    method: 'get',
    params: {
      id: id,
      url: url
    }
  });
};